﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.IO;
using System.Threading;

namespace RpgEditor
{

    class ImgFile
    {
        public Image img;
        public String fn;
    }
    public class SpriteEditor : Control
    {
        

        public ArrayList spriteImg = new ArrayList();
        //public  SByte[][] tmpFragments;
        public ArrayList tmpFragments = new ArrayList();
        public ArrayList sprites = new ArrayList();
        public ArrayList tmpFrame = new ArrayList();

        mainForm form;
        private String fn = null;

        public bool displayspritestatus = false;
        public int imgrate = 1;
        public int selectsprite = -1;
        public int curfragindex = -1;

        public void init()
        {
            setFn(null);
            spriteImg = new ArrayList();
            tmpFragments = new ArrayList();
            sprites = new ArrayList();
            tmpFrame = new ArrayList();
        }
        public void setFn(String value)
        {
            fn = value;
            form.textBox1.Text = fn;
        }
        public String getFn()
        {
            return fn;
        }
        public SpriteEditor(mainForm form)
        {
            this.form = form;
        }
        
        public void displayFrameList(int flag)
        {
            int select = form.listBox4.SelectedIndex;
                form.listBox4.Items.Clear();
                // 显示
                if (this.tmpFrame != null)
                {
                    for (int i = 0; i < this.tmpFrame.Count; i++)
                    {
                        String s = ""+i;
                        form.listBox4.Items.Add(s);
                    }
                    if (form.listBox4.Items.Count > 0)
                    {
                        if (flag == 0)
                        {
                            form.listBox4.SelectedIndex = 0;
                        }
                        if (flag == 1 && select < form.listBox4.Items.Count)
                        {
                            form.listBox4.SelectedIndex = select;
                            if (select < 0)
                            {
                                form.listBox4.SelectedIndex = 0;
                            }
                        }
                        if (flag == 2)
                        {
                            form.listBox4.SelectedIndex = form.listBox4.Items.Count - 1;
                        }

                    }
                }
        }
        public void display()
        {
            form.listBox1.Items.Clear();
            form.listBox2.Items.Clear();


            displayImgList();
            displayFragList(true);
            displayFrameList(1);
            displaySpriteList(1);
        }

        public void displayImgList()
        {
            Graphics g = form.panel24.CreateGraphics();

            g.Clear(form.BackColor);
            form.listBox2.Items.Clear();
            if (spriteImg != null)
            {
                Point p = new Point();
                p.X = 0;
                p.Y = 0;
                for (int i = 0; i < spriteImg.Count; i++)
                {
                    ImgFile iif = (ImgFile)spriteImg[i];
                    Image ti = iif.img;
                    g.DrawImage(ti, new Rectangle(p.X, p.Y, ti.Width, ti.Height));
                    p.X = 0;
                    p.Y = p.Y + ti.Height + 40;

                    Pen pen = new Pen(Color.Blue);
                    g.DrawLine(pen, 0, p.Y - 20, 200, p.Y - 20);
                    form.listBox2.Items.Add(Path.GetFileNameWithoutExtension(iif.fn));

                    form.listBox2.SelectedIndex = 0;
                }

                
            }
        }
        public void displayFragList(bool flag)
        {
            int select = form.listBox3.SelectedIndex;
            form.listBox3.Items.Clear();
            // 关联片段
            if (form.listBox2.SelectedIndex != -1 && tmpFragments != null && tmpFragments.Count > form.listBox2.SelectedIndex)
            {

                // 显示
                ArrayList tl = (ArrayList)(tmpFragments[form.listBox2.SelectedIndex]);
                int size = tl.Count / 4;

                for (int i = 0; i < size; i++)
                {
                    String s = "";
                    int k = i * 4;

                    for (int j = k; j < k + 4; j++)
                    {
                        sbyte ttt = (sbyte)(tl[j]);
                        if (j == (k + 3))
                        {
                            // 最后一个

                            s = s + (Byte)(ttt);
                        }
                        else
                        {
                            s = s + (Byte)(ttt) + ",";
                        }
                    }
                    form.listBox3.Items.Add(s);
                }

                // draw 切片


                // 显示
                Graphics g = form.panel24.CreateGraphics();
                 tl = (ArrayList)(tmpFragments[form.listBox2.SelectedIndex]);
                 size = tl.Count / 4;
                 Rectangle rect;
                 Pen pen = new Pen(Color.Blue, 0.5f);
                for (int i = 0; i < size; i++)
                {
                    String s = "";
                    int k = i * 4;

                    for (int j = k; j < k + 4; j++)
                    {
                        if (j == (k + 3))
                        {
                            // 最后一个
                            s = s + (SByte)(tl[j]);
                        }
                        else
                        {
                            s = s + (SByte)(tl[j]) + ",";
                        }
                    }
                    int h1 = ((SByte)(tl[k++]) ) * imgrate;
                    int w1 = ((SByte)(tl[k++]) ) * imgrate;
                    int x1 = ((SByte)(tl[k++])) * imgrate;
                    int y1 = ((SByte)(tl[k++])) * imgrate;
                    rect = new Rectangle(x1, y1, w1, h1);
                    if (form.listBox3.SelectedIndex != -1 && i == form.listBox3.SelectedIndex)
                    {
                        pen.Color = Color.Red;
                    }
                    else
                    {
                        pen.Color = Color.Blue;
                    }
                    g.DrawRectangle(pen, rect);
                }

            }

            if (flag)
            {
                if (select < form.listBox3.Items.Count)
                    form.listBox3.SelectedIndex = select;

            }
            else
            {
                if (form.listBox3.Items.Count > 0)
                {
                    form.listBox3.SelectedIndex = 0;
                }
            }
            
        }
        public Rectangle getIndexFrag(int x, int y)
        {
            Rectangle rect = new Rectangle(0, 0, -1, -1);
            if (form.listBox2.SelectedIndex != -1 && tmpFragments != null && tmpFragments.Count > form.listBox2.SelectedIndex)
            {
                ArrayList tl = (ArrayList)(tmpFragments[form.listBox2.SelectedIndex]);
                int size = tl.Count / 4;
                
                for (int i = 0; i < size; i++)
                {
                    int k = i * 4;
                    int h1 = ((SByte)(tl[k++])) * imgrate;
                    int w1 = ((SByte)(tl[k++])) * imgrate;
                    int x1 = (Byte)(((SByte)(tl[k++]))) * imgrate;
                    int y1 = (Byte)(((SByte)(tl[k++]))) * imgrate;
                    rect = new Rectangle(x1, y1, w1, h1);
                    if (rect.Contains(x, y))
                    {
                        curfragindex = i;
                        return rect;
                    }
                }

            }
            rect = new Rectangle(0, 0, -1, -1);
            return rect;
        }

        public void drawfrag()
        {
            if (form.listBox2.SelectedIndex != -1 && tmpFragments != null && tmpFragments.Count > form.listBox2.SelectedIndex)
            {

                   // draw 切片

                displayBigImg();
                // 显示
                Graphics g = form.panel24.CreateGraphics();
                
                ArrayList tl = (ArrayList)(tmpFragments[form.listBox2.SelectedIndex]);
                int size = tl.Count / 4;
                Rectangle rect;
                Pen pen = new Pen(Color.Blue, 0.5f);
                for (int i = 0; i < size; i++)
                {
                    String s = "";
                    int k = i * 4;
                   
                    for (int j = k; j < k + 4; j++)
                    {
                        sbyte ttt = (sbyte)(tl[j]);
                        if (j == (k + 3))
                        {
                            // 最后一个
                            s = s + (Byte)(ttt);
                        }
                        else
                        {
                            s = s + (Byte)(ttt) + ",";
                        }
                    }
                    int h1 = ((SByte)(tl[k++])) * imgrate;
                    int w1 = ((SByte)(tl[k++])) * imgrate;
                    int x1 = (Byte)(((SByte)(tl[k++]))) * imgrate;
                    int y1 = (Byte)(((SByte)(tl[k++]))) * imgrate;
                    rect = new Rectangle(x1, y1, w1, h1);
                    if (form.listBox3.SelectedIndex != -1 && i == form.listBox3.SelectedIndex)
                    {
                        pen.Color = Color.Red;
                    }
                    else
                    {
                        pen.Color = Color.Blue;
                    }
                    g.DrawRectangle(pen, rect);
                }

            }
        }

        public void displayBigImg()
        {
            if (spriteImg != null)
            {
                
                Graphics g = form.panel24.CreateGraphics();
                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
                g.Clear(form.BackColor);
                ImgFile iif = (ImgFile)spriteImg[form.listBox2.SelectedIndex];
                Image ti = iif.img;
                int h = ti.Height;
                int w = ti.Width;
                Rectangle rect = new Rectangle(imgrate/2, imgrate/2, w * imgrate,h*imgrate);
                Pen pen = new Pen(Color.Blue, 0.5f);
                pen.Color = Color.Blue;
                g.DrawImage(ti, rect);
                rect.X = 0;
                rect.Y = 0;
                g.DrawRectangle(pen, rect);

                
                
            }
        }
        public void displaySpriteList(int flag)
        {
            int select = form.listBox1.SelectedIndex;
            form.listBox1.Items.Clear();
            if (sprites != null)
            {                
                for (int i = 0; i < sprites.Count; i++)
                {
                    Sprite s = (Sprite)(sprites[i]);
                    form.listBox1.Items.Add(s.name);
                }
                if (form.listBox1.Items.Count > 0)
                {
                    if (flag == 0)
                    {
                        form.listBox1.SelectedIndex = 0;
                    }
                    if (flag == 1 && select < form.listBox1.Items.Count)
                    {
                        form.listBox1.SelectedIndex = select;
                        if (select < 0)
                        {
                            form.listBox1.SelectedIndex = 0;
                        }
                    }
                    if (flag == 2)
                    {
                        form.listBox1.SelectedIndex = form.listBox1.Items.Count - 1;
                    }
                }
            }
        }
        
        //线程主循环
        public void run(object obj)
        {

        }
        public void displaySprite()
        {
           // int select = form.listBox1.SelectedIndex;
            Graphics g = form.panel3.CreateGraphics();
            Sprite s = (Sprite)(sprites[selectsprite]);
            displayspritestatus = true;
            while (true && displayspritestatus)
            {
                for (int si = 0; si < s.frameLength; si++)
                {
                    ArrayList frame = (ArrayList)(s.frames[si]);
                    displayFrame(g, frame,false);
                    Thread.Sleep(200);
                    Rectangle rect1 = new Rectangle();
                    rect1.X = 0;
                    rect1.Y = 0;
                    rect1.Height = form.panel3.Height;
                    rect1.Width = form.panel3.Width;
                    g.SetClip(rect1);
                    g.Clear(form.BackColor);

                }
                if (!form.checkBox1.Checked)
                    break;
            }
            displayspritestatus = false;
        }

        private void drawFrameCenter()
        {
            Graphics g = form.panel3.CreateGraphics();
            int ph = form.panel3.Height;
            int pw = form.panel3.Width ;
            Pen pen = new Pen(Color.Black, 0.5f);
            g.DrawLine(pen, 0, ph/2, pw, ph / 2);
            g.DrawLine(pen, pw / 2, 0, pw / 2, ph);
        }

       public ArrayList frameFragRect;
        public void displayFrame(Graphics g, ArrayList frame,bool edit)
        {
            int fragmentNum = frame.Count;
            int ph = form.panel3.Height /2;
            int pw = form.panel3.Width /2;
            int rate = 1;
            if (edit)
            {
                rate = imgrate;
            }
            Pen pen = new Pen(Color.Red, 1f);
            frameFragRect = new ArrayList();
            drawFrameCenter();
            for (int i = 0; i < fragmentNum; i++)
            {                
                ArrayList tl1 = (ArrayList)(frame[i]);

                int cacheid = (SByte)(tl1[0]);
                int fragmentindex = ((SByte)(tl1[1]) & 0xff)
                        | ((SByte)(tl1[2]) & 0xff) << 8;
                int transx = (SByte)(tl1[3])*rate + pw;
                //transx = transx * rate;
                int transy = (SByte)(tl1[4])*rate + ph;
               // transy = transy * rate;
                // 用这行代码也可以
                //ArrayList fragment = (ArrayList)s.fragments[cacheid];
                ArrayList fragment = (ArrayList)tmpFragments[cacheid];

                int h = (SByte)(fragment[fragmentindex++]);
                int w = (SByte)(fragment[fragmentindex++]);
                h = h * rate;
                w = w * rate;
                SByte ttt = (SByte)(fragment[fragmentindex++]);
                int offsetx = (Byte)ttt;
                offsetx = offsetx * rate;
                ttt = (SByte)(fragment[fragmentindex]);
                int offsety = (Byte)ttt;
                offsety = offsety * rate;

                Rectangle rect = new Rectangle();
                rect.X = transx;
                rect.Y = transy;
                rect.Height = h;
                rect.Width = w;
                g.SetClip(rect);
                frameFragRect.Add(rect);
                ImgFile iif = (ImgFile)spriteImg[cacheid];
                Image ti = iif.img;
                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
                rect = new Rectangle(transx - offsetx, transy - offsety, ti.Width * rate, ti.Height * rate);
                g.DrawImage(ti, rect);
                
                //g.DrawRectangle(pen, rect);
               
            }
            if (edit && form.checkBox2.Checked)
            {
                g.SetClip(new Rectangle(0, 0, form.panel3.Width, form.panel3.Height));
                for (int i = 0; i < fragmentNum; i++)
                {
                    Rectangle rect = (Rectangle)frameFragRect[i];

                    g.DrawRectangle(pen, rect);
                }
            }
        }

        static ArrayList readFragment(BinaryReader dis)
        {
            int size = MyIO.readByte(dis);
            int length = 4;
            ArrayList buff = new ArrayList();
            // id,h,w,offsetx,offsety
            for (int i = 0; i < size; i++)
            {
                int k = i * length;
                MyIO.readByte(dis); // id,not use
                for (int j = k; j < k + length; j++)
                { // h,w,offsetx,offsety
                    //buff[j] = MyIO.readByte(dis);
                    buff.Add(MyIO.readByte(dis));
                }
            }
            return buff;
        }

        static void writeFragment(BinaryWriter dis, ArrayList buff)
        {
            int size = 0;
            if (buff != null)
            {
                size = buff.Count / 4;
            }

            MyIO.writeByte(dis, (SByte)size);
            int length = 4;

            // id,h,w,offsetx,offsety
            for (int i = 0; i < size; i++)
            {
                int k = i * length;
                MyIO.writeByte(dis, (SByte)i); // id,not use
                for (int j = k; j < k + length; j++)
                { // h,w,offsetx,offsety
                    //buff[j] = MyIO.readByte(dis);
                    MyIO.writeByte(dis, (SByte)(buff[j]));
                }
            }

        }


        // 和读取是反向的操作
        public void Save()
        {
            FileStream fs1 = new FileStream(this.fn, FileMode.Create, FileAccess.Write);
            BinaryWriter dis = new BinaryWriter(fs1, System.Text.Encoding.Default);
            MyIO.writeByte(dis, (SByte)spriteImg.Count);
            for (int i = 0; i < spriteImg.Count; i++)
            {
                ImgFile iif = (ImgFile)(spriteImg[i]);
                String name = Path.GetFileNameWithoutExtension(iif.fn);
                MyIO.writeByte(dis, (SByte)i);
                MyIO.writeString(dis, name);

                String dir = Path.GetDirectoryName(this.fn);

                FileStream fs2 = new FileStream(dir + "/" + name + ".frg", FileMode.Create, FileAccess.Write);
                BinaryWriter dis2 = new BinaryWriter(fs2);
                if (i >= tmpFragments.Count)
                {
                    writeFragment(dis2, null);
                }
                else
                {
                    writeFragment(dis2, (ArrayList)(tmpFragments[i]));
                }
                FileStream fs3 = new FileStream(iif.fn, FileMode.Open);
                BinaryReader dis3 = new BinaryReader(fs3);
                Byte[] b = new Byte[fs3.Length];
                b = dis3.ReadBytes((int)fs3.Length);
                dis2.Write(b);
                dis3.Close();
                dis2.Close();
            }

            // 开始写每一帧数据
            MyIO.writeByte(dis, (SByte)tmpFrame.Count);
            for (int i = 0; i < tmpFrame.Count; i++)
            {
                MyIO.writeByte(dis, (SByte)i);

                // 该帧使用到的fragment
                ArrayList tl = (ArrayList)(tmpFrame[i]);
                MyIO.writeByte(dis, (SByte)tl.Count);
                for (int j = 0; j < tl.Count; j++)
                {
                    ArrayList tll = (ArrayList)(tl[j]);
                    // cacheid                    
                    MyIO.writeByte(dis, (SByte)tll[0]);
                    // fragmentid in the cache
                    int fragmentindex = ((SByte)(tll[1]) & 0xff)
                            | ((SByte)(tll[2]) & 0xff) << 8;

                    MyIO.writeByte(dis, (SByte)(fragmentindex / 4));
                    // paint to the frame's x coordinates
                    MyIO.writeByte(dis, (SByte)tll[3]);
                    MyIO.writeByte(dis, (SByte)tll[4]);

                }
            }

            // 开始写每一Sprite

            MyIO.writeByte(dis, (SByte)sprites.Count);
            for (int i = 0; i < sprites.Count; i++)
            {
                Sprite sprite = (Sprite)sprites[i];
                MyIO.writeString(dis, sprite.name);
                //SByte gestureSize = MyIO.readByte(dis);
                MyIO.writeByte(dis, (SByte)(sprite.frameLength));
                for (int j = 0; j < sprite.frameLength; j++)
                {
                    // 根据值写frames的索引值
                    // MyIO.writeByte(dis, (SByte)(j));
                    MyIO.writeByte(dis, (SByte)(sprite.framesindex[j]));
                }
            }
            dis.Close();

        }
        /**
    * 加载精灵,若精灵所使用的图片已经存在则不加载,所有精灵使用共享的Fragment缓冲区以及Image缓冲区
    */
        public void readObject(String fn)
        {
            //init();
            FileStream fs1 = new FileStream(fn, FileMode.Open, FileAccess.Read);
            BinaryReader dis = new BinaryReader(fs1, System.Text.Encoding.Default);
            int imgNum = MyIO.readByte(dis);
            spriteImg = new ArrayList(imgNum);
            // 填充内容
            for (int i = 0; i < imgNum; i++)
            {
                spriteImg.Add("");
            }
            tmpFragments = new ArrayList(imgNum);
            for (int i = 0; i < imgNum; i++)
            {
                tmpFragments.Add(new ArrayList());
            }
            for (int i = 0; i < imgNum; i++)
            {

                SByte id = MyIO.readByte(dis);

                String name = MyIO.readString(dis);
                String dir = Path.GetDirectoryName(fn);

                FileStream fs2 = new FileStream(dir + "/" + name + ".frg", FileMode.Open);
                BinaryReader dis2 = new BinaryReader(fs2);

                tmpFragments[id] = readFragment(dis2);

                Byte[] imgbyte = new Byte[fs2.Length - fs2.Position];
                int len = (int)(fs2.Length - fs2.Position);
                imgbyte = dis2.ReadBytes((len));
                dis2.Close();
                dis2 = null;

                MemoryStream ms = new MemoryStream(imgbyte, 0, len);
                ms.Position = 0;

                Image ti = Image.FromStream(ms,true);
                
                ImgFile iif = new ImgFile();
                dir = dir + "/temp/";
                iif.fn = dir + name + ".png";

                if (!Directory.Exists(dir))
                {
                    Directory.CreateDirectory(dir);
                }
                iif.img = ti;
                spriteImg[id] = iif;
                //ti.Save(iif.fn,System.Drawing.Imaging.ImageFormat.Png);
                if (!File.Exists(iif.fn))
                {
                    FileStream fs3 = new FileStream(iif.fn, FileMode.Create);
                    BinaryWriter dis3 = new BinaryWriter(fs3);
                    dis3.Write(imgbyte);
                    dis3.Close();
                }
            }

            // 开始读取每一帧数据

            int frameNum = (Byte)(dis.ReadByte());
            tmpFrame = new ArrayList();
            for (int i = 0; i < frameNum; i++)
            {
                tmpFrame.Add(new ArrayList());
            }
            // SByte[][] tmpCollide = new SByte[frameNum][4];
            for (int i = 0; i < frameNum; i++)
            {
                int id = (Byte)(dis.ReadByte());
                // 该帧的碰撞参考点
                // tmpCollide[id][0] = dis.ReadByte();
                // tmpCollide[id][1] = dis.ReadByte();
                // tmpCollide[id][2] = dis.ReadByte();
                // tmpCollide[id][3] = dis.ReadByte();

                // 该帧使用到的fragment
                int fragmentNum = (Byte)(dis.ReadByte());
                //tmpFrame[id] = new SByte[fragmentNum][];
                tmpFrame[id] = new ArrayList();
                for (int j = 0; j < fragmentNum; j++)
                {
                    ArrayList tl = new ArrayList();
                    ArrayList tll = (ArrayList)(tmpFrame[id]);
                    tll.Add(tl);

                    // cacheid
                    tl.Add(MyIO.readByte(dis));
                    // fragmentid in the cache
                    short fragmentid = (short)(MyIO.readByte(dis) * 4);
                    tl.Add((SByte)(fragmentid & 0xff)); // fragmentid_low_8
                    tl.Add((SByte)((fragmentid >> 8) & 0xff)); // fragmentid_top_8
                    // paint to the frame's x coordinates
                    tl.Add((SByte)MyIO.readByte(dis));
                    // paint to the frame's y coordinates
                    tl.Add((SByte)MyIO.readByte(dis));
                }
            }

            // 开始读取每一Sprite
            int spriteNum = MyIO.readByte(dis);
            sprites = new ArrayList(spriteNum);

            for (int i = 0; i < spriteNum; i++)
            {
                String name = MyIO.readString(dis);
                SByte gestureSize = MyIO.readByte(dis);
                Sprite sprite = new Sprite(name, gestureSize);
                for (int j = 0; j < gestureSize; j++)
                {
                    int index = (Byte)MyIO.readByte(dis);
                    // 设置精灵的帧系列
                    sprite.frames[j] = tmpFrame[index];
                    sprite.framesindex[j] = index;
                    // sprite.collideVertex[j] = tmpCollide[index];
                }
                // 设置该精灵使用到的所有图片资源
                sprite.imgs = spriteImg;
                sprite.fragments = tmpFragments;
                sprites.Add(sprite);
            }
            dis.Close();

        }

        public void addImg(String fn)
        {
            ImgFile iif = new ImgFile();
            iif.fn = fn;

            FileStream fs2 = new FileStream(fn, FileMode.Open);
            BinaryReader dis2 = new BinaryReader(fs2);
            Byte[] imgbyte = new Byte[fs2.Length];
            int len = (int)(fs2.Length);
            imgbyte = dis2.ReadBytes((len));
            dis2.Close();
            dis2 = null;

            MemoryStream ms = new MemoryStream(imgbyte, 0, len);
            ms.Position = 0;

            Image ti = Image.FromStream(ms);
            iif.img = ti;
            spriteImg.Add(iif);

            tmpFragments.Add(new ArrayList());
        }
        public bool changeFrag(int imgindex, int fragindex, String s)
        {
            ArrayList tl = (ArrayList)(tmpFragments[imgindex]);


            int length = 4;

            // id,h,w,offsetx,offsety

            int k = fragindex * length;
            String[] sl = s.Split(new Char[] { ',' });
            if (sl.Length != 4)
            {
                return false;
            }
            for (int j = k,i= 0; j < k + length; j++)
            {
                tl[j] = (SByte)(Int16.Parse(sl[i]));
                i++;
            }
            return true;
        }

        public bool addNullFrag(int imgindex)
        {

            ArrayList tl;
            
            tl= (ArrayList)(tmpFragments[imgindex]);


            int length = 4;

            
            for (int j = 0; j < length; j++)
            {
                tl.Add((SByte)0);
            }
            return true;
        }
        public bool addNullFrame()
        {
            tmpFrame.Add(new ArrayList());
            return true;
        }

        public String[] getFrameValue(int index)
        {
            ArrayList tl = (ArrayList)(tmpFrame[index]);
            String[] result = new String[tl.Count];
            for (int j = 0; j < tl.Count; j++)
            {
                ArrayList tll = (ArrayList)(tl[j]);
                String s = "";
                // cacheid
                s = s + tll[0] + ",";
                // fragmentid in the cache
                int fragmentindex = ((SByte)(tll[1]) & 0xff)
                            | ((SByte)(tll[2]) & 0xff) << 8;
                short fragmentid = (short)(fragmentindex / 4);
                s = s + fragmentid + ",";
                // paint to the frame's x coordinates
                SByte offsetx = (SByte)tll[3];
                s = s + (SByte)offsetx + ",";
                // paint to the frame's y coordinates
                SByte offsety = (SByte)tll[4];
                s = s + (SByte)offsety;
                result[j] = s;
            }
            return result;
        }
        public bool changeFrame(int index, String[] sl)
        {
            ArrayList tl = (ArrayList)(tmpFrame[index]);
            tl.Clear();
            for (int i = 0; i < sl.Length; i++)
            {
                String s = sl[i];
                String[] ts = s.Split(new Char[] { ',' });
                if (ts.Length != 4)
                {
                    return false;
                }
                ArrayList tll = new ArrayList();
                tl.Add(tll);

                tll.Add(SByte.Parse(ts[0]));
                //
                short fragmentid = (short)(Int16.Parse(ts[1]) * 4);
                tll.Add((SByte)(fragmentid & 0xff)); // fragmentid_low_8
                tll.Add((SByte)((fragmentid >> 8) & 0xff)); // fragmentid_top_8

                tll.Add(SByte.Parse(ts[2]));
                tll.Add(SByte.Parse(ts[3]));
            }
            return true;
        }

        public bool addSprite(String name)
        {
            Sprite s = new Sprite(name, 0);
            sprites.Add(s);
            return true;
        }

        public bool changeSprite(int index,String name,String frames)
        {
            Sprite s = (Sprite)sprites[index];
            s.name = name;
            String[] ts = frames.Split(new Char[] { ',' });
            s.framesindex = new int[ts.Length];
            s.frames = new ArrayList();
            s.frameLength = (SByte)ts.Length;
            for (int i = 0; i < ts.Length; i++)
            {
                int ii = Int32.Parse(ts[i]);
                s.framesindex[i] = ii;
                s.frames.Add(tmpFrame[ii]);
            }
            s.imgs = spriteImg;
            s.fragments = tmpFragments;
            return true;
        }

        public void deleteimg(int index)
        {
            // 删除imglist
            if (spriteImg != null && index < spriteImg.Count)
            {
                spriteImg.RemoveAt(index);
            }
            // 删除相关切片
            if (tmpFragments != null && index < tmpFragments.Count)
            {
                tmpFragments.RemoveAt(index);
            }
            // 删除相关frame
            if (tmpFrame != null)
            {
                for (int i = tmpFrame.Count - 1; i >= 0; i--)
                {
                    ArrayList frame = (ArrayList)tmpFrame[i];
                    for (int j = frame.Count-1; j >=0; j--)
                    {
                        ArrayList tl1 = (ArrayList)(frame[j]);
                        if ((sbyte)tl1[0] == index)
                        {
                            frame.RemoveAt(j);
                        }
                    }
                    if (frame.Count <= 0)
                    {
                       // tmpFrame.RemoveAt(i);
                    }
                }
            }
        }
    }






}
